#-*-coding: utf-8-*-

# Version: 0.1
# Author: Don Li <iedon@126.com>
# License: Copyright(c) 2017 Don.Li
# Summary:

import osutil
import os, sys, time, multiprocessing

from ctypes  import *
from os.path import pardir, dirname
from log     import log

path = ''.join((dirname(__file__), '/', pardir, '/', pardir, '/lib/func.so') if osutil.run_with_source() else (osutil.root_path(), '/func.so'))

if not os.path.isfile(path):
    raise Exception('func.so not exists. path: {0}'.format(path))

def process(request):
    fault           = int(request.get('fault', '0'))
    func_so         = CDLL(path)
    cprint          = func_so.cprint_fault if fault == 1 else func_so.cprint_normal
    cprint.argtypes = [c_char_p]
    cprint.restype  = c_char_p

    res = cprint('Hello, {0}'.format(request).encode('utf-8')).decode()
    if fault == 2:
        x = 1/0
    elif fault == 3:
        time.sleep(3)

    return res
